﻿using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraTreeList;
using XCI.Data.Entity;
using XCI.Windows.Helper;

namespace XCI.Windows.Plugin.GridSetting
{
    /// <summary>
    /// 表格配置插件接口
    /// </summary>
    public interface IGridSettingPlugin
    {
        /// <summary>
        /// 标识
        /// </summary>
        string Id { get; }

        /// <summary>
        /// 目标对象
        /// </summary>
        object Target { get; }

        /// <summary>
        /// 实体元数据
        /// </summary>
        EntityData Metadata { get; }

        /// <summary>
        /// 自动生成列
        /// </summary>
        void AutoBuildColumn();

        /// <summary>
        /// 获取表格配置
        /// </summary>
        string GetSetting();

        /// <summary>
        /// 设置表格配置
        /// </summary>
        /// <param name="setting">配置对象</param>
        void SetSetting(string setting);
    }

    public class GridSettingPlugin : IGridSettingPlugin
    {
        public GridView View { get; }
        public string Id { get; }
        public object Target { get; }
        public EntityData Metadata { get; }

        public GridSettingPlugin(GridView view, string id, EntityData metadata)
        {
            View = view;
            Target = view;
            Id = id;
            Metadata = metadata;
        }

        public void AutoBuildColumn()
        {
            WinHelper.AutoBuildGridColumn(View, Metadata);
        }

        public string GetSetting()
        {
            return WinHelper.GetGridViewSetting(View);
        }

        public void SetSetting(string setting)
        {
            WinHelper.SetGridViewSetting(View, setting);
        }
    }

    public class TreeSettingPlugin : IGridSettingPlugin
    {
        public TreeList Tree { get; }
        public string Id { get; }
        public object Target { get; }
        public EntityData Metadata { get; }

        public TreeSettingPlugin(TreeList tree, string id, EntityData metadata)
        {
            Tree = tree;
            Target = tree;
            Id = id;
            Metadata = metadata;
        }

        public void AutoBuildColumn()
        {
            WinHelper.AutoBuildTreeColumn(Tree, Metadata);
        }

        public string GetSetting()
        {
            return WinHelper.GetTreeSetting(Tree);
        }

        public void SetSetting(string setting)
        {
            WinHelper.SetTreeSetting(Tree, setting);
        }
    }
}


//// ===============================================================================
//// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
//// ===============================================================================
//using DevExpress.Utils.Menu;
//using DevExpress.XtraTreeList;
//using System.Windows.Forms;
//using XCI.Windows.Controls;
//using XCI.Windows.Plugin.GridSetting.UI;

//namespace XCI.Windows.Plugin.GridSetting
//{
//    /// <summary>
//    /// 树形表格配置插件
//    /// </summary>
//    public class TreeListSettingPlugin
//    {
//        private bool _isExpandAll;
//        private bool _isCheckedAll;

//        /// <summary>
//        /// 初始化控件时执行
//        /// </summary>
//        /// <param name="tree">树形控件</param>
//        public void OnInit(TreeListx tree)
//        {
//            if (!tree.AllowSettingGrid) return;
//            var data = GridSettingManager.Instance.LoadSetting(tree.Id);
//            tree.SetSetting(data);
//            tree.PopupMenuShowing += (o, e) =>
//            {
//                TreeListHitInfo hitInfo = tree.CalcHitInfo(e.Point);
//                if (hitInfo.HitInfoType != HitInfoType.Column) return;
//                var menu = e.Menu;
//                var column = hitInfo.Column;
//                var item = new DXMenuItem("全部展开/合上", (oo, ex) =>
//                {
//                    if (!_isExpandAll)
//                    {
//                        tree.ExpandAll();
//                        _isExpandAll = true;
//                    }
//                    else
//                    {
//                        tree.CollapseAll();
//                        _isExpandAll = false;
//                    }
//                });
//                item.BeginGroup = true;
//                menu.Items.Add(item);
//                item = new DXMenuItem("全选/清除", (oo, ex) =>
//                {
//                    if (!_isCheckedAll)
//                    {
//                        tree.CheckAll();
//                        _isCheckedAll = true;
//                    }
//                    else
//                    {
//                        tree.UncheckAll();
//                        _isCheckedAll = false;
//                    }
//                });
//                item.BeginGroup = true;
//                menu.Items.Add(item);

//                if (column != null)
//                {
//                    DXMenuItem sortItem = new DXMenuItem("清除排序", (oo, ee) =>
//                    {
//                        column.SortDir = SortDir.None;
//                        tree.RefreshDataSource();
//                    }, null);
//                    sortItem.Enabled = false;
//                    sortItem.BeginGroup = true;
//                    menu.Items.Add(sortItem);
//                    if (column.SortDir != SortDir.None)
//                    {
//                        sortItem.Enabled = true;
//                    }
//                }

//                //if (IsShowRecycleBinButton && Metadata != null && !string.IsNullOrEmpty(Metadata.IsDelete))
//                //{
//                //    DXMenuItem recycleBinItem = new DXMenuItem("回收站",
//                //    (oo, a) => ShowRecycleBinForm(), XCI.WinControls.Properties.Resources.recyclebin16);
//                //    recycleBinItem.BeginGroup = true;
//                //    menu.Items.Add(recycleBinItem);
//                //}
//                //DXMenuItem customItem = new DXMenuItem("保存配置", (oo, aa) =>
//                //{
//                //    if (tree.AllowSettingGrid)
//                //    {
//                //        GridSettingManager.Instance.SaveSetting(tree.Id, tree.GetSetting());
//                //    }
//                //});
//                //customItem.BeginGroup = true;
//                //menu.Items.Add(customItem);

//                var customItem = new DXMenuItem("表格配置", (oo, aa) => ShowSettingForm(tree),
//                    XCI.Windows.Properties.Resources.Configure16);
//                customItem.BeginGroup = true;
//                menu.Items.Add(customItem);

//            };

//            tree.KeyUp += (s, e) =>
//            {
//                if (e.Control && e.Shift && e.Alt && e.KeyCode == Keys.F12)
//                {
//                    ShowSettingForm(tree);
//                    e.Handled = true;
//                }
//            };
//        }

//        /// <summary>
//        /// 显示配置窗口
//        /// </summary>
//        private void ShowSettingForm(TreeListx tree)
//        {
//            var settingFrm = new TreeConfigForm();
//            settingFrm.Initialize(tree);
//            settingFrm.ShowDialog();
//            settingFrm.Dispose();
//        }
//    }
//}


//// ===============================================================================
//// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
//// ===============================================================================
//using System.Windows.Forms;
//using DevExpress.Utils.Menu;
//using DevExpress.XtraGrid.Menu;
//using DevExpress.XtraGrid.Views.Grid;
//using XCI.Windows.Controls;
//using XCI.Windows.Plugin.GridSetting.UI;

//namespace XCI.Windows.Plugin.GridSetting
//{
//    /// <summary>
//    /// 表格配置插件
//    /// </summary>
//    public class GridSettingPlugin
//    {
//        /// <summary>
//        /// 初始化控件时执行
//        /// </summary>
//        /// <param name="view">表格视图控件</param>
//        public void OnInit(GridViewEx view)
//        {
//            if (!view.AllowLoadSetting) return;
//            var data = GridSettingManager.Instance.LoadSetting(view.Id);
//            view.SetSetting(data);
//            view.PopupMenuShowing += (s, e) =>
//            {
//                if (e.MenuType != GridMenuType.Column) return;
//                GridViewColumnMenu menu = e.Menu as GridViewColumnMenu;
//                if (menu == null) return;

//                //DXMenuItem customItem = new DXMenuItem("保存配置", (oo, aa) =>
//                //{
//                //    GridSettingManager.SaveSetting(view.Id, view.GetSetting());
//                //});
//                //customItem.BeginGroup = true;
//                //menu.Items.Add(customItem);

//                var customItem = new DXMenuItem("表格配置", (o, a) => ShowSettingForm(view.GridControlEx),
//                    XCI.Windows.Properties.Resources.Configure16);
//                customItem.BeginGroup = true;
//                menu.Items.Add(customItem);
//            };

//            view.KeyDown += (s, e) =>
//            {
//                if (e.Control && e.Shift && e.Alt && e.KeyCode == Keys.F12)
//                {
//                    ShowSettingForm(view.GridControlEx);
//                    e.Handled = true;
//                }
//            };
//        }

//        /// <summary>
//        /// 显示配置窗口
//        /// </summary>
//        private void ShowSettingForm(GridControlEx grid)
//        {
//            //var settingFrm = new GridConfigForm();
//            //settingFrm.Initialize(grid);
//            //settingFrm.ShowDialog();
//            //settingFrm.Dispose();
//        }
//    }
//}